package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.ContractPayLogAddDTO;
import org.jsola.contract.dto.ContractPayLogUpdateDTO;
import org.jsola.contract.query.ContractPayLogQuery;
import org.jsola.contract.service.IContractPayLogService;
import org.jsola.contract.vo.ContractPayLogListVO;
import org.jsola.contract.vo.ContractPayLogVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 租户电子合同充值记录
 *
 * @author JanusMix
 */
@RestController("contractContractPayLogController")
@RequestMapping(ContractConstants.API_PATH + "/v1/contractPayLogs")
public class ContractPayLogController implements IContractPayLogControllerAPI {

    @Autowired
    private IContractPayLogService contractPayLogService;

    @Override
    @PostMapping
    public Result<ContractPayLogVO> save(@Valid @RequestBody ContractPayLogAddDTO contractPayLogAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增租户电子合同充值记录权限校验

        return Result.success(
                contractPayLogService.save(contractPayLogAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ContractPayLogUpdateDTO contractPayLogUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改租户电子合同充值记录权限校验

        int count = contractPayLogService.update(contractPayLogUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{contractPayLogId}")
    public Result<Integer> delete(@PathVariable Long contractPayLogId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除租户电子合同充值记录权限校验

        int count = contractPayLogService.deleteByIds(tokenUser, contractPayLogId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] contractPayLogIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除租户电子合同充值记录权限校验

        int count = contractPayLogService.deleteByIds(tokenUser, contractPayLogIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{contractPayLogId}")
    public Result<ContractPayLogVO> findById(@PathVariable Long contractPayLogId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户电子合同充值记录权限校验

        return Result.success(
            contractPayLogService.selectById(contractPayLogId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ContractPayLogListVO>> listAll(@Valid ContractPayLogQuery contractPayLogQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户电子合同充值记录权限校验

        return Result.success(
            contractPayLogService.select(contractPayLogQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ContractPayLogListVO>> page(@Valid ContractPayLogQuery contractPayLogQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            contractPayLogService.selectPage(contractPayLogQuery, tokenUser.getSiteId())
        );
    }


}

